package com.chen.palmar.project.init;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.entity.AuthEntity;
import com.chen.palmar.entity.LoginEntity;
import com.primb.androidlibs.net.converter.RxSchedulerUtils;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.ui.PasswordEditText;
import com.primb.androidlibs.utils.RegexUtils;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.schedulers.Schedulers;

/**
 * Created by chen on 2017/12/28.
 */

public class RegisterActivity extends BaseActivity {

    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_auth_code)
    EditText etAuthCode;
    @Bind(R.id.tv_auth_code)
    TextView tvAuthCode;
    @Bind(R.id.et_pwd)
    PasswordEditText etPwd;
    @Bind(R.id.btn_register)
    Button btnRegister;
    private boolean isRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        init();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void init() {
        isRegister = getIntent().getBooleanExtra("isRegister", true);
        if (isRegister) {
            btnRegister.setText("注册");
            etPwd.setHint("请输入密码");
        } else {
            btnRegister.setText("确定");
            etPwd.setHint("设置新密码");
        }
    }

    @OnClick({R.id.tv_auth_code, R.id.btn_register, R.id.tv_login, R.id.tv_login1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_auth_code:
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    showToast("请输入手机号");
                    return;
                }
                if (!RegexUtils.isMobileExact(etPhone.getText())) {
                    showToast("请输入正确格式手机号");
                    return;
                }
                getAuthCode(etPhone.getText().toString());
                break;
            case R.id.btn_register:
                if (TextUtils.isEmpty(etPhone.getText().toString()) || TextUtils.isEmpty(etAuthCode.getText().toString()) || TextUtils.isEmpty(etPwd.getText().toString())) {
                    showToast("信息填写不完整，请填写后重试");
                    return;
                }
                registerAccount(etPhone.getText().toString(), etAuthCode.getText().toString(), etPwd.getText().toString());
                break;
            case R.id.tv_login:
            case R.id.tv_login1:
                this.finish();
                break;
        }

    }

    private void registerAccount(String phone, String auth, String pwd) {
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("code_type", isRegister ? "2" : "3");
        map.put("password", pwd);
        map.put("verify_code", auth);
        if (isRegister) {
            Subscription register = DataCenter.register(map).subscribe(new HttpSubscriber<LoginEntity>(this, showLoading("加载中")) {
                @Override
                public void onNext(LoginEntity loginEntity) {
                    DataCenter.httpConfig.setLoginToken(loginEntity.getData().getAnycode());
                    Intent intent = new Intent(RegisterActivity.this, IdentityActivity.class);
                    startActivity(intent);
                    RegisterActivity.this.finish();
                }
            });
            subscription.add(register);
        } else {
            Subscription register = DataCenter.targetPwd(map).subscribe(new HttpSubscriber<HttpResultEntity>(this, showLoading("加载中")) {
                @Override
                public void onNext(HttpResultEntity loginEntity) {
                    showToast(loginEntity.getMessage());
                    RegisterActivity.this.finish();
                }
            });
            subscription.add(register);
        }
    }

    private void getAuthCode(String phone) {
        tvAuthCode.setEnabled(false);
        Subscription subscribe = Observable.interval(1, TimeUnit.SECONDS, Schedulers.io()).take(59).compose(RxSchedulerUtils.normalSchedulersTransformer()).subscribe(new Subscriber<Long>() {
            @Override
            public void onCompleted() {
                tvAuthCode.setEnabled(true);
                tvAuthCode.setText("重新获取验证码");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Long aLong) {
                tvAuthCode.setText((59 - aLong) + "秒");
            }
        });
        subscription.add(subscribe);

        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("code_type", isRegister ? "2" : "3");
        Subscription auth = DataCenter.getAuthCode(map).subscribe(new HttpSubscriber<AuthEntity>(this, null) {
            @Override
            public void onNext(AuthEntity o) {
                if (!TextUtils.isEmpty(o.getMessage())) {
                    showToast(o.getMessage());
                }
            }
        });
        subscription.add(auth);
    }
}
